package com.zty.xiaomi.server.Controller;

import com.zty.xiaomi.server.Dto.*;
import com.zty.xiaomi.server.Entity.Tribunal.Tribunal;
import com.zty.xiaomi.server.Entity.User;
import com.zty.xiaomi.server.Service.Good.GoodServiceImp;
import com.zty.xiaomi.server.Service.Order.OrderServiceImp;
import com.zty.xiaomi.server.Service.Tribunal.TribunalServiceImp;
import com.zty.xiaomi.server.Service.User.UserServiceImpl;
import com.zty.xiaomi.server.utils.RSAUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("tribunal")
public class TribunalController {
    @Autowired
    private TribunalServiceImp tribunalServiceImp;

//    @Autowired
//    private OrderServiceImp orderServiceImp;

    @Autowired
    private GoodServiceImp goodServiceImp;

    @Autowired
    private UserServiceImpl userServiceImp;



    @PostMapping("addTribunal")
    private AddTribunalDto getAllService(@RequestBody Tribunal tribunal) throws IOException {
        AddTribunalDto addTribunalDto = new AddTribunalDto();
        tribunal.setStore_id(goodServiceImp.getStoreIdByGoodId(tribunal.getGood_id()));
        String timestamp = String.valueOf(new Date().getTime());
        int time = Integer.valueOf(timestamp.substring(0,timestamp.length()-3).trim());
        tribunal.setCreate_time(time);
        int num = tribunalServiceImp.addTribunal(tribunal);
        if (num == 1) {
            addTribunalDto.setMsg("小法庭开庭成功！");
            addTribunalDto.setStatus("200");
            addTribunalDto.setTribunal_id(tribunal.getTribunal_id());
        } else {
            addTribunalDto.setMsg("小法庭开庭失败！");
            addTribunalDto.setStatus("400");
        }
        return addTribunalDto;
    }

    @GetMapping("getTribunalsByStatus")
    private GetTribunalInfoDto getTribunalsByStatus(@RequestParam("status") int status) throws IOException {
        GetTribunalInfoDto getTribunalInfoDto = new GetTribunalInfoDto();
        List<Tribunal> list = tribunalServiceImp.getTribunalsByStatus(status);
        if (list.size() == 0){
            getTribunalInfoDto.setStatus("400");
            getTribunalInfoDto.setMsg("该状态下无案件信息！");
        } else {
            getTribunalInfoDto.setList(list);
            getTribunalInfoDto.setStatus("200");
        }
        return getTribunalInfoDto;
    }

    @GetMapping("updateTribunalById")
    private UpdateTribunalInfoDto updateTribunalById(@RequestParam("tribunal_id") int tribunal_id,
                                                  @RequestParam("vote") int vote,
                                                  @RequestParam("user_name") String user_name) throws IOException {
        UpdateTribunalInfoDto updateTribunalInfoDto = new UpdateTribunalInfoDto();
        User user = userServiceImp.getUserInfoByName(user_name);
        if (user == null) {
            updateTribunalInfoDto.setStatus("400");
            updateTribunalInfoDto.setMsg("用户信息异常！请重新登录");
        }
        if (user.getVote_status() != 0) {
            updateTribunalInfoDto.setMsg("投票失败！您有投票的案件尚未结案！");
            updateTribunalInfoDto.setStatus("200");
            return updateTribunalInfoDto;
        }
        Tribunal tribunal = tribunalServiceImp.getTribunalDetailById(tribunal_id);
        if (isOverVote(tribunal)) {
            updateTribunalInfoDto.setMsg("投票失败！投票已结束！");
            updateTribunalInfoDto.setStatus("200");
        } else {
            int num = 0;
            if (vote == 0) {
                num = tribunalServiceImp.updateTribunalUserVoteById(tribunal_id);
            } else {
                num = tribunalServiceImp.updateTribunalStoreVoteById(tribunal_id);
            }
            if (num == 1) {
                updateTribunalInfoDto.setStatus("200");
                updateTribunalInfoDto.setMsg("投票成功！");
                int number = userServiceImp.updateUserVoteStatus(user.getName(), tribunal_id);
                if (number == 0) {
                    updateTribunalInfoDto.setMsg("投票成功！用户投票资格更新失败！");
                }
            } else {
                updateTribunalInfoDto.setStatus("500");
                updateTribunalInfoDto.setMsg("投票失败！");
            }
        }

        tribunal = tribunalServiceImp.getTribunalDetailById(tribunal_id);
        if (tribunal.getUser_vote() == 2) {
            tribunalServiceImp.updateTribunalResultAndStatusById(tribunal_id
                    ,"买家胜诉", 2);
            int num = userServiceImp.updateUserVoteStatusByTribunalId(tribunal_id);
        } else {
            tribunalServiceImp.updateTribunalResultAndStatusById(tribunal_id
                    ,"卖家胜诉", 2);
            int num = userServiceImp.updateUserVoteStatusByTribunalId(tribunal_id);
        }

        return updateTribunalInfoDto;
    }

    public boolean isOverVote(Tribunal tribunal) throws IOException {
        if (tribunal.getStatus() == 2){
            return true;
        } else {
            return false;
        }
    }

    @GetMapping("getTribunalDetailById")
    private GetTribunalDetialDto getTribunalById(@RequestParam("tribunal_id") int tribunal_id) throws IOException {
        GetTribunalDetialDto getTribunalDetialDto = new GetTribunalDetialDto();
        Tribunal tribunal = tribunalServiceImp.getTribunalDetailById(tribunal_id);
        if (tribunal == null) {
            getTribunalDetialDto.setStatus("400");
            getTribunalDetialDto.setMsg("该案件信息获取异常！");
        }
        getTribunalDetialDto.setData(tribunal);
        getTribunalDetialDto.setStatus("200");
        return getTribunalDetialDto;
    }

    @GetMapping("getMyTribunalByUserName")
    private GetMyTribunalInfoDto getMyTribunalByUserName(@RequestParam("user_name") String user_name) throws IOException {
        GetMyTribunalInfoDto getMyTribunalInfoDto = new GetMyTribunalInfoDto();
        List<Tribunal> list = tribunalServiceImp.getMyTribunalByUserName(user_name);
        if (list.size() == 0) {
            getMyTribunalInfoDto.setStatus("400");
            getMyTribunalInfoDto.setMsg("暂无小法庭案件记录！！");
        } else {
            getMyTribunalInfoDto.setList(list);
            getMyTribunalInfoDto.setStatus("200");
        }
        return getMyTribunalInfoDto;
    }
}
